
package sskj.regulatory.presentation.adapter

import android.support.v4.content.ContextCompat
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import sskj.regulatory.R
import sskj.regulatory.presentation.screen.sitevideo.siteveiw.SiteViewFragment

/**
 * @author Anthony Fermin (Fuzz)
 */
class DropDownAdapter(private val actionViewDelegate: ViewActions) : RecyclerView.Adapter<DropDownAdapter.StandViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StandViewHolder {
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.list_item_stand_drop_down, parent, false)
        return StandViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: StandViewHolder, position: Int) {
        holder.bind(position)
    }

    override fun getItemCount(): Int {
        return actionViewDelegate.getItemCount()
    }

    inner class StandViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        private var standTitleTV: TextView = itemView.findViewById<View>(R.id.cell_stand_title) as TextView


        private val standViewItemClickListener = View.OnClickListener {
            val lastSelectedPosition = actionViewDelegate.getSelectedStand()
            actionViewDelegate.setSelectedStand(adapterPosition)
            notifyItemChanged(lastSelectedPosition)
            notifyItemChanged(adapterPosition)
            actionViewDelegate.collapseDropDown()
            actionViewDelegate.stopAndPlayLiveVideo()
        }

        init {
            itemView.setBackgroundDrawable(ContextCompat.getDrawable(itemView.context, R.drawable.stand_drop_down_selector))
            itemView.setOnClickListener(standViewItemClickListener)
        }

        fun bind(position: Int) {
            standTitleTV.text = actionViewDelegate.getStandTitle(position)
            itemView.isSelected = (actionViewDelegate.getSelectedStand() == position)
        }
    }

    interface ViewActions {
        fun collapseDropDown()
        fun getStandTitle(standId: Int): String
        fun getSelectedStand(): Int
        fun setSelectedStand(standId: Int)
        fun getItemCount():Int
        fun stopAndPlayLiveVideo()
    }
}

